package com.wawayaya.bookreader.stroke;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.wawayaya.bookreader.bean.ShortPoint;
import com.wawayaya.bookreader.bean.StrokeRecord;

import java.util.Vector;

public class StrokeRecordLoader {
	private static final String TAG = StrokeRecordLoader.class.getSimpleName();
	private static final String STROKE_URI = "content://com.wawayayareader.strokeprovider/stroke/";
	private static final String COLUMN_CODE = "code";
	private static final String COLUMN_INDEX = "strokeindex";
	private static final String COLUMN_NAME = "strokename";
	private static final String COLUMN_PTX = "x";
	private static final String COLUMN_PTY = "y";
	private static final String COLUMN_TYPE = "type";
	private static final String COLUMN_ANGLE = "endAngle";
	private static final String COLUMN_OUTLINE = "outline";
	private static final String COLUMN_MIDLINE = "midline";
    private static final String COLUMN_LIST[] = new String[]{COLUMN_INDEX, COLUMN_NAME, COLUMN_PTX, COLUMN_PTY, COLUMN_TYPE, COLUMN_ANGLE, COLUMN_OUTLINE, COLUMN_MIDLINE};
	
	public static Vector<StrokeRecord> loadFromContentProvider(ContentResolver cr, String strChn) {
		// TODO Auto-generated method stub
		Vector<StrokeRecord> vsr;
		String strQuery = STROKE_URI + getCodeID(strChn);
        Uri allCalls = Uri.parse(strQuery);
        Cursor cur = cr.query(allCalls, COLUMN_LIST, null, null, null);
        vsr = loadFromCursor(cur, strChn);
    	if (cur != null)
    		cur.close();
		return vsr;
	}
	
	protected static Vector<StrokeRecord> loadFromCursor(Cursor cur, String strChn) {
        if (cur == null || !cur.moveToFirst())
        	return null;
    	int[] colIndex = new int[COLUMN_LIST.length];
    	for(int i=0; i<COLUMN_LIST.length; i++)
    		colIndex[i] = cur.getColumnIndex(COLUMN_LIST[i]);
    	Vector<StrokeRecord> vsr = new Vector<StrokeRecord>();
    	do{
    		StrokeRecord sr = new StrokeRecord();
    		sr.mCode = strChn;
    		sr.mContourIndex = cur.getInt(colIndex[0]);
    		sr.mStrokeName = cur.getString(colIndex[1]);
    		sr.mStartX = cur.getInt(colIndex[2]);
    		sr.mStartY = cur.getInt(colIndex[3]);
    		int nType = cur.getInt(colIndex[4]);
    		if ((nType & 0x01) != 0)
    			sr.mBuShou = true;
    		else
    			sr.mBuShou = false;
    		if ((nType & 0x02) != 0)
    			sr.mStrokeEnd = false;
    		else
    			sr.mStrokeEnd = true;
    		sr.mEndAngle = cur.getInt(colIndex[5]);
    		byte[] bData1 = cur.getBlob(colIndex[6]);
    		sr.mContourLine = loadShortPoint(bData1);
    		byte[] bData2 = cur.getBlob(colIndex[7]);
    		sr.mMiddleLine = loadShortPoint(bData2);
    		vsr.add(sr);
    	}while(cur.moveToNext());
		return vsr;
	}

	public static Vector<StrokeRecord> loadFromFile(Context context, String sPath, String strChn) {
		SQLiteDatabase db = SQLiteDatabase.openDatabase(sPath, null, SQLiteDatabase.OPEN_READONLY);
		if (db == null)
			return null;
		Vector<StrokeRecord> vsr;
		String strSel = String.format("%s='%s'", COLUMN_CODE, strChn);
		Cursor cur = db.query("tb_stroke", COLUMN_LIST, strSel, null, "", "", "");
		vsr = loadFromCursor(cur, strChn);
		if (cur != null)
			cur.close();
		db.close();
		return vsr;
	}
	
	private static int getCodeID(String strCode) {
		int n = 0;
		try{
			byte[] bData = strCode.getBytes("GBK");
			n = (int)(bData[0] & 0xff);
			int m = (int)(bData[1] & 0xff);
			n += (m << 8);
		}catch(Exception e){
			Log.d(TAG, "" + e.getMessage());
			n = 0;
		}
		return n;
	}

	public static ShortPoint[] loadShortPoint(byte[] bData) {
		if (bData == null || (bData.length % 4) != 0 || bData.length <= 0)
			return null;
		int nCount = bData.length / 4;
		ShortPoint[] psp = new ShortPoint[nCount];
		for(int i=0; i<nCount; i++) {
			psp[i] = new ShortPoint();
			psp[i].x = loadShort(bData, i*4 + 0);
			psp[i].y = loadShort(bData, i*4 + 2);
		}
		return psp;
	}
	
	protected static short loadShort(byte[] bData, int nIndex) {
		int n = (int)(bData[nIndex+0] & 0xff);
		int m = (int)(bData[nIndex+1] & 0xff);
		return (short)(n + (m << 8));
	}
}
